
/*    This file is part of the managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanaintlib.runtime.utils;

import org.managarm.jmanavalib.core.VaDatatype;
import org.managarm.jmanaintlib.machine.VmCell;
import org.managarm.jmanaintlib.machine.VmCellArray;
import org.managarm.jmanaintlib.machine.VmCellChar;
import org.managarm.jmanaintlib.machine.VmError;

public class RtStrings {
	public static String virtualToJavaString(VmCell cell)
			throws VmError {
		final int size = (int)cell.arraySize();
		final char[] chars = new char[size];
		for(int i = 0; i < size; i++)
			chars[i] = (char)cell.loadElement(i).valueAsChar();
		return new String(chars);
	}
	
	public static VmCell javaToVirtualString(String string)
			throws VmError {
		final VaDatatype.DtArray type = new VaDatatype.DtArray
			(new VaDatatype.DtPrimitive(VaDatatype.DtPrimitive.PRIM_CHAR), 0);
		final VmCellArray array = new VmCellArray(type, string.length());
		for(int i = 0; i < string.length(); i++)
			array.storeElement(i, new VmCellChar(string.charAt(i)));
		return array;
	}
}
